package com.jhx.controller.user;

import com.github.pagehelper.PageInfo;
import com.jhx.entity.Result;
import com.jhx.pojo.SysUser;
import com.jhx.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 跳转主页
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpSession session, Model model){
        SysUser user =(SysUser) session.getAttribute("USER_SESION");
        model.addAttribute("name",user.getName());
        model.addAttribute("id",user.getId());
        return "main";
    }

    /**
     * 跳转首页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";

    }
    /**
     * 登录验证
     * @return
     */
    @RequestMapping("/login")
    public String login(SysUser sysUser, Model model , HttpSession session){
        System.out.println("登录验证");
        System.out.println(sysUser);
        if(sysUser.getUsername()!=null && sysUser.getPassword()!=null){
            System.out.println(sysUser.getUsername());
            System.out.println("----------------------------");
            System.out.println(sysUser.getPassword());
            SysUser user = sysUserService.findUserByName(sysUser);
            if (user!=null){
                session.setAttribute("USER_SESION", user);
                model.addAttribute("name",user.getName());
                model.addAttribute("id",user.getId());
                return "main";
            }
        }
        model.addAttribute("msg","用户名或密码错误");
            return "login";
    }


    /**
     * 查询用户(默认所有)
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("/user_list")
    public String userList(String username,Model model,
                           @RequestParam(value = "pageMum", defaultValue = "1") Integer pageMum,
                           @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize){
        List<SysUser> allUsers = sysUserService.getAllUsers(username,pageMum,pageSize);
        PageInfo<SysUser> pageInfo = new PageInfo<>(allUsers);
        model.addAttribute("userList",allUsers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("username",username);
        return "user_list";
    }

    /**
     *修改用户信息
     * @param sysUser
     * @return
     */
    @RequestMapping("/add_user")
    @ResponseBody
    public Result<String> addUser(SysUser sysUser){
        Result<String> result = new Result<>();
        result.setMsg("请填写用户名和密码");
        if (sysUser!=null
                && sysUser.getUsername()!=null
                &&!sysUser.getUsername().trim().equals("")
                && sysUser.getPassword()!=null
                &&!sysUser.getPassword().trim().equals("")
        ){
            SysUser user = sysUserService.getUserByName(sysUser.getUsername());
            if (user==null) {
                int i = sysUserService.addUser(sysUser);
                if (i > 0) {
                    System.out.println("添加成功");
                    result.setMsg("添加成功");
                    result.setSuccess(true);
                    return result;
                } else {
                    System.out.println("添加失败");
                    result.setMsg("添加失败");
                    result.setSuccess(false);
                    return result;
                }
            }else {
                System.out.println("用户名已存在");
                result.setMsg("用户名已存在");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delete_user/{id}")
    @ResponseBody
    public Result<String> deleteUser (@PathVariable Integer id){

            int i = sysUserService.deleteUser(id);
            if (i > 0) {
                System.out.println("删除成功");
                return new Result<>("删除成功", null, true);

            }
            System.out.println("删除失败");
            return new Result<>("删除失败",null,false);
    }

    /**
     * 批量删除
     */
    @RequestMapping("/allDelete_user")
    @ResponseBody
    public Result<String> allDeleteUsers(String ids){
        Result<String> result = new Result<>();
        System.out.println("批量删除");
        System.out.println(ids);

        if (ids!=null && !ids.trim().equals("")){
            String[] strArray = ids.split(",");
            List<String> list = Arrays.asList(strArray);
            System.out.println(list);
            int i = sysUserService.allDeleteUsers(list);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择删除的用户！");
            result.setMsg("请选择删除的用户！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @RequestMapping("/get_user")
    @ResponseBody
    public Result getUserById(Integer id ,Model model){

        Result<SysUser> result = new Result<>();
        SysUser userById = sysUserService.getUserById(id);
        if (userById!=null){
            System.out.println("查找成功！");
            result.setMsg("查找成功！");
            result.setData(userById);
            result.setSuccess(true);
            return result;
        }else {
            System.out.println("查找失败！");
            result.setMsg("查找失败！");
            result.setSuccess(false);
            return result;
        }

    }

    @RequestMapping("/update_user")
    @ResponseBody
    public Result<String> updateUser(SysUser sysUser){
        Result<String> result = new Result<>();
        result.setMsg("请填写用户名和密码");
        if (sysUser!=null
                && sysUser.getId()!=null
                && sysUser.getUsername()!=null
                &&!sysUser.getUsername().trim().equals("")
        ){
            SysUser user = sysUserService.getUserByName(sysUser.getUsername());
            if (user==null) {
                int i = sysUserService.updateUser(sysUser);
                if (i > 0) {
                    System.out.println("修改成功");
                    result.setMsg("修改成功");
                    result.setSuccess(true);
                    return result;
                } else {
                    System.out.println("修改失败");
                    result.setMsg("修改失败");
                    result.setSuccess(false);
                    return result;
                }
            }else {
                System.out.println("请更新用户名");
                result.setMsg("请更新用户名");
                return result;
            }
        }
        return result;
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("/output")
    public String output(HttpSession session){
        System.out.println("退出");
        session.removeAttribute("USER_SESION");
        return "login";
    }

    /**
     * 修改密码
     * @param sysUser
     * @return
     */
    @RequestMapping("/update_password")
    @ResponseBody
    public Result<String> updateUser1(SysUser sysUser){
        Result<String> result = new Result<>();
        result.setMsg("请填写新密码");
        if (    sysUser!=null
                && sysUser.getId()!=null
                && sysUser.getPassword()!=null
                &&!sysUser.getPassword().trim().equals("")
        ){
            int i = sysUserService.updateUser(sysUser);
            if (i > 0) {
                System.out.println("修改成功");
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("修改失败");
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }


}
